package com.cabrida.livestar.main;

import com.cabrida.livestar.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * 底部状态栏
 * 
 * @author why
 */
public class IndicatorMainView extends LinearLayout implements OnClickListener {
	private Context mContext;
	private RelativeLayout layoutHomePage;
	private ImageView ivHomePage;
	private TextView tvHomePage;

	private RelativeLayout layoutStar_circle;
	private ImageView ivStar_circle;
	private TextView tvStar_circle;
	
	private RelativeLayout layoutVlog;
	private ImageView ivVlog;
	private TextView tvVlog;

	private RelativeLayout layoutMine;
	private ImageView ivMine;
	private TextView tvMine;

	private static int COLOR_UNSELECT = 0;
	private static int COLOR_SELECT = 0;

	public static final int VIEW_HOMEPAGE = 0;
	public static final int VIEW_STAR_CIRCLE= 1;
	public static final int VIEW_VLOG = 2;
	public static final int VIEW_MINE = 3;
	public int mCurIndicator = VIEW_HOMEPAGE;

	public IndicatorMainView(Context context) {
		super(context);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_indicator, this);
		Init(view);
	}

	public IndicatorMainView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_indicator, this);
		Init(view);
	}

	/**
	 * 初始化控件
	 */
	private void Init(View view) {
		COLOR_UNSELECT = mContext.getResources().getColor(R.color.text_999);
		COLOR_SELECT = mContext.getResources().getColor(R.color.green);

		layoutHomePage = (RelativeLayout) view
				.findViewById(R.id.layout_indicator_homepage);
		ivHomePage = (ImageView) view.findViewById(R.id.iv_indicator_homepage);
		tvHomePage = (TextView) view.findViewById(R.id.tv_indicator_homepage);

		layoutStar_circle = (RelativeLayout) view
				.findViewById(R.id.layout_indicator_star_circle);
		ivStar_circle = (ImageView) view.findViewById(R.id.iv_indicator_star_circle);
		tvStar_circle = (TextView) view.findViewById(R.id.tv_indicator_star_circle);
		
		layoutVlog = (RelativeLayout) view
		.findViewById(R.id.layout_indicator_vlog);
		ivVlog = (ImageView) view.findViewById(R.id.iv_indicator_vlog);
		tvVlog = (TextView) view.findViewById(R.id.tv_indicator_vlog);

		layoutMine = (RelativeLayout) view
				.findViewById(R.id.layout_indicator_mine);
		ivMine = (ImageView) view.findViewById(R.id.iv_indicator_mine);
		tvMine = (TextView) view.findViewById(R.id.tv_indicator_mine);
		layoutHomePage.setOnClickListener(this);
		layoutStar_circle.setOnClickListener(this);
		layoutVlog.setOnClickListener(this);
		layoutMine.setOnClickListener(this);

	}

	public void setIndicator(int which) {
		ivHomePage.setImageResource(R.drawable.home);
		ivStar_circle.setImageResource(R.drawable.star_circle);
		ivVlog.setImageResource(R.drawable.vlog);
		ivMine.setImageResource(R.drawable.home_my);
		
		tvHomePage.setTextColor(COLOR_UNSELECT);
		tvStar_circle.setTextColor(COLOR_UNSELECT);
		tvVlog.setTextColor(COLOR_UNSELECT);
		tvMine.setTextColor(COLOR_UNSELECT);
		
		mCurIndicator = which;
		switch (which) {
		case VIEW_HOMEPAGE:
			tvHomePage.setTextColor(COLOR_SELECT);
			ivHomePage.setImageResource(R.drawable.home_o);
			break;
		case VIEW_STAR_CIRCLE:
			tvStar_circle.setTextColor(COLOR_SELECT);
			ivStar_circle.setImageResource(R.drawable.star_circle_o);
			break;
		case VIEW_VLOG:
			tvVlog.setTextColor(COLOR_SELECT);
			ivVlog.setImageResource(R.drawable.vlog_o);
			break;
		case VIEW_MINE:
			tvMine.setTextColor(COLOR_SELECT);
			ivMine.setImageResource(R.drawable.home_my_o);
			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_indicator_homepage:
			if (mCurIndicator != VIEW_HOMEPAGE) {
				mOnIndicateListener.onIndicate(v, VIEW_HOMEPAGE);
				setIndicator(VIEW_HOMEPAGE);
			}
			break;
		case R.id.layout_indicator_star_circle:
			if (mCurIndicator != VIEW_STAR_CIRCLE) {
				mOnIndicateListener.onIndicate(v, VIEW_STAR_CIRCLE);
				setIndicator(VIEW_STAR_CIRCLE);
			}
			break;
		case R.id.layout_indicator_vlog:
			if (mCurIndicator != VIEW_VLOG) {
				mOnIndicateListener.onIndicate(v, VIEW_VLOG);
				setIndicator(VIEW_VLOG);
			}
			break;
		case R.id.layout_indicator_mine:
			if (mCurIndicator != VIEW_MINE) {
				mOnIndicateListener.onIndicate(v, VIEW_MINE);
				setIndicator(VIEW_MINE);
			}
			break;

		default:
			break;
		}
	}

	private OnIndicateListener mOnIndicateListener;

	public interface OnIndicateListener {
		public void onIndicate(View v, int which);
	}

	public void setOnIndicateListener(OnIndicateListener listener) {
		mOnIndicateListener = listener;
	}

}
